package client.GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

/**
 * Errorwindow for error displaying purposes.
 * @author SIIM
 *
 */
public class ErrorWindow extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel panel;
	private JPanel buttonPanel;
	private JButton ok;
	private JLabel error;
	private JFrame mw;
	
	public ErrorWindow(String errorMsg, JFrame mw) {
		this.mw = mw;
		setUndecorated(true);
		mw.setEnabled(false);
		Point p = mw.getLocation();
		Dimension d = mw.getSize();
		panel = new JPanel();
		buttonPanel = new JPanel();
		ok = new JButton("OK");
		error = new JLabel("    " + errorMsg + "    ");
		
		panel.setLayout(new BorderLayout());
		panel.setBorder(BorderFactory.createRaisedBevelBorder());
		panel.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "enter");
		panel.getActionMap().put("enter", new okAction());
		ok.addActionListener(new okListener());
		error.setFont(new Font(null, 1, 14));
		
		
		buttonPanel.add(ok);
		panel.add(error, BorderLayout.CENTER);
		panel.add(buttonPanel, BorderLayout.SOUTH);
		add(panel);
		
		pack();
		p.setLocation(p.getX() + ((d.getWidth() - this.getWidth()) / 2), p.getY() + (d.getHeight() / 4));
		setLocation(p);
		this.setVisible(true);
	}
	/**
	 * React to enter.
	 * @author SIIM
	 *
	 */
	private class okListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			doActions();
		}
		
	}
	/**
	 * React to enter.
	 * @author SIIM
	 *
	 */
	private class okAction extends AbstractAction {

		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent arg0) {
			doActions();
			
		}
		
	}
	/**
	 * Help method for doing actions.
	 */
	private void doActions() {
		mw.setEnabled(true);
		ErrorWindow.this.dispose();
	}

}
